﻿

using System;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

namespace Gameplay.CharacterFramework
{
    public class SkillManager : MonoBehaviour
    {
        [SerializeField] private List<Skill> _skillList = new List<Skill>();

        private Dictionary<int, Skill> _skillDictionary = new Dictionary<int, Skill>();

        [HideInInspector]
        public Skill CurrentUseSkill;

        public SkillConfig CurrentConfig => CurrentUseSkill?.Config;

        [HideInInspector]
        public Role User;
        private void Start()
        {
            //RegisterSkill();
        }

        private void RegisterSkill()
        {
            foreach (Skill skill in _skillList)
            {
                _skillDictionary.Add(skill.Config.Id, skill);
            }
        }
        public void RegisterSkill(Weapon currentWeapon)
        {
            _skillDictionary.Clear();
            foreach (var skill in currentWeapon.Config.Skills)
            {
                if (_skillDictionary.ContainsKey(skill.Config.Id)) 
                {
                    continue;
                }
                
                skill.CurrentWeapon = currentWeapon;
                _skillDictionary.Add(skill.Config.Id, skill);
            } 
        }
        public void Init(Role character)
        {
            User = character;

            if (CurrentUseSkill == null) 
            {
                CurrentUseSkill = _skillList[0];
            }
        }
        public void Use(int id)
        {
            if (!_skillDictionary.ContainsKey(id))
            {
                Debug.Log("未找到该技能");
                return;
            }
            CurrentUseSkill = _skillDictionary[id];
        }

        /// <summary>
        /// 没有目标的技能
        /// </summary>
        /// <param name="id">技能id</param>
        public void UseSkill(int id)
        {
            if (!_skillDictionary.ContainsKey(id))
            {
                Debug.Log("未找到该技能");
                return;
            }
            CurrentUseSkill = _skillDictionary[id];

            CurrentUseSkill.Use(User);
        }

        /// <summary>
        /// 有目标的技能
        /// </summary>
        /// <param name="id">技能id</param>
        /// <param name="target">目标</param>
        public void UseSkill(int id,Role target) 
        {
            if (!_skillDictionary.ContainsKey(id))
            {
                Debug.Log("未找到该技能");
                return;
            }
            CurrentUseSkill = _skillDictionary[id];
            CurrentUseSkill.Use(User,target);
        }

        public void OnSkill()
        {
            if (CurrentUseSkill != null)
            {
                CurrentUseSkill.Begin();
            }
        }

        public void SkillTrigger()
        {
            if (CurrentUseSkill != null)
            {
                CurrentUseSkill.Trigger();
            }
        }

        public void SkillFinish()
        {
            if (CurrentUseSkill != null)
            {
                CurrentUseSkill.Finish();
            }
        }

        public void SkillEnd()
        {
            if (CurrentUseSkill != null)
            {
                CurrentUseSkill.End();
            }
        }

        public void SkillUpdate()
        {
            if (CurrentUseSkill != null)
            {
                CurrentUseSkill.Update();
            }
        }

        public void CanelSkill()
        {
            if (CurrentUseSkill != null)
            {
                CurrentUseSkill.Canel();
            }
        }

        
    }
}
